home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_1 / state.spa / comp.sys.handhelds_2671_000000.msg
Internet Message Format  |  1995-03-23  |  12KB

  1. Path: en.ecn.purdue.edu!noose.ecn.purdue.edu!samsung!usc!apple!agate!shelby!portia.stanford.edu!elaine2.stanford.edu!mcgrant
  2. From: mcgrant@elaine2.stanford.edu (Michael Grant)
  3. Newsgroups: comp.sys.handhelds
  4. Subject: STATE-SPACE SYSTEM manipulation programs
  5. Keywords: resolvents, polynomial manipulation, and so forth.
  6. Message-ID: <1990Dec1.231507.19959@portia.Stanford.EDU>
  7. Date: 1 Dec 90 23:15:07 GMT
  8. Sender: news@portia.Stanford.EDU
  9. Reply-To: mcgrant@elaine2.stanford.edu (Michael Grant)
  10. Followup-To: comp.sys.,handhelds
  11. Organization: AIR, Stanford University
  12. Lines: 329
  13.  
  14. The following are a set of programs which accomplish a lot for
  15. me in the way of state-space system manipulation, as well as a
  16. few programs which could be separated from this list to handle
  17. polynomials and the like.  The functions that they perform
  18. include:
  19. 1) transfer function calculation
  20. 2) (which means calculation of det(sI-A) and adj(sI-A))
  21. 3) state feedback gain calculations
  22. 4) observability matrix and controllability matrix determination
  23. 5) manual root finding (in other words, I GUESS, the HP tries it out)
  24. and so forth.
  25.  
  26. NOTE: AN UNCOMMENTED SET OF THESE PROGRAMS HAS BEEN POSTED IN ANOTHER
  27. POST, SO DON'T WORRY ABOUT EDITING TOO MUCH...
  28.  
  29. A lot of people wanted the resolvent (sI-A)^-1 program, so I decided
  30. to post the whole darn thing.  KEEP IN MIND that these were written
  31. on an HP28s, but I don't think that there are any major compatability
  32. hassles here since we're not dealing with screen manipulation and such.i
  33.  
  34. Sorry, but while I use these often and I'm almost positive they are correct,
  35. I can't be held responsible for typos.  BUT, I'll certainly be glad to
  36. hear from you if you find a problem with any of these programs, because
  37. I want to correct them myself!
  38.  
  39. Send comments, suggestions, money, fame, etc. to
  40. mcgrant@portia.stanford.edu
  41.  
  42. Enjoy...(in a manner of speaking)
  43. Michael C. Grant
  44. Information Systems Lab
  45.  
  46. ---
  47. I recommend using the following ORDER command:
  48. { A B C D INITIALIZE TFNCAL ADJ OBSR CNTR FBGN SNDV POLY
  49.   EPOLY POLCH PREM POST tmp idt ns no ni } ORDER
  50.  
  51. The following four variables are the global variables which
  52. contain the matrices representing your state-space realization
  53. { A,b,c,d }.  They must agree in dimension for the follwing
  54. programs to work properly, and INITIALIZE checks this.  In
  55. short, as long as they are meaningful in the vector equation
  56.      dx/dt=Ax+Bu, y=Cx+Du, then they are valid.
  57. [[-2 -3 -4]
  58.  [ 1  0  0]
  59.  [ 0  1  0]]
  60. 'A' STO
  61. [1 0 0]
  62. 'B' STO
  63. [[3 5 7]]
  64. 'C' STO
  65. 0
  66. 'D' STO
  67.  
  68. INITIALIZE--confirms the integrity of A,B,C,&D, and sets up the
  69.             temporary variables that the rest of the system uses.
  70. INPUTS: none
  71. OUTPUTS: a string, which may contain one or several of the following:
  72.          "OK" -- all of the dimensions match and the system is ready.
  73.          "AS" -- A is not square.
  74.          "AB" -- the rows of A and the rows of B do not match.
  75.          "AC" -- the columns of A and the columns of C do not match.
  76.          "BD" -- the columns of D and the columns of B do not match.
  77.          "CD" -- the rows of C and the rows of D do not match.
  78. COMMENTS: Most of the programs will bomb or give incorrect results if
  79.           this program is not run (or does ot return "OK").  I decided
  80.           that running this program once is worth the extra nuisance
  81.           with respect to the speed savings it generates in the other
  82.           programs by eliminating type checking and pre-initializing
  83.           the global variables in the system.
  84. <<
  85.    << IF DUP TYPE 1 <=
  86.       THEN DROP 1 1
  87.       ELSE SIZE LIST-> 1 == 1 IFT
  88.       END 
  89.    >> -> P
  90.    << A P EVAL B P EVAL C P EVAL D P EVAL -> AR AC BR BC CR CC DR DC
  91.       << ""
  92.          IF AR AC <>
  93.          THEN "AS" +
  94.          END
  95.          IF BR BC <>
  96.          THEN "AB" +
  97.          END
  98.          IF AC CC <>
  99.          THEN "AC" +
  100.          END
  101.          IF BC DC <>
  102.          THEN "BD" +
  103.          END
  104.          IF CR DR <>
  105.          THEN "CD"
  106.          END
  107.          IF DUP SIZE 0 ==
  108.          THEN OK + AR 'ns' STO DC 'ni' STO DR 'no' STO
  109.               { ns ns } 0 CON 'tmp' STO ns IDN 'idt' STO
  110.          END
  111.       >>
  112.    >>
  113. >>
  114.  
  115. TFNCAL--calculates C[(sI-A)^-1]B
  116. INPUTS: none (assumes INITIALIZE returned "OK")
  117. OUTPUTS: 2: the polynomial list for the numerator, Cadj(sI-A)B
  118.          1: the polynomial list for the denominator, det(sI-A)
  119.         [ remember, C[(sI-A)^-1]B = Cadj(sI-A)B/det(sI-A) ]
  120. COMMENTS: note that in the multi-input and multi-output cases,
  121.           the polynomial in level 2 will have matrix coefficients!
  122.           The consequence of this is the POLY will not work for it,
  123.           but EPOLY can give you values for individual choices of s.
  124.           BUT, in most cases people can use POLCH to choose a
  125.           single transfer function polynomial from the matrix.
  126.           This program (as well as the ADJ program) uses the method
  127.           found in Thomas Kailath, _Linear Systems_, Prentice-Hall,
  128.           Englewood Cliffs, N.J, (c) 1980, p. 657, better (or worse)
  129.           known as the Levverier-Souriau-Faddeeva-Frame formulas.
  130. << IF D ABS
  131.    THEN D 1
  132.    ELSE 0
  133.    END ->LIST { 1 } 'tmp' 0 CON 1 -> ai 
  134.    << 1 ns
  135.       FOR J ai idt * 'tmp' STO+ SWAP C tmp B * *
  136.          IF 1 no == 1 ni == AND
  137.          THEN 1 GET
  138.          END D 'tmp' A STO* 0 1 ns
  139.             FOR K 'tmp' { K K } GET +
  140.             NEXT J NEG / DUP 'ai' STO * + + SWAP ai +
  141.       NEXT
  142.    >>
  143. >>
  144.  
  145. ADJ--calculates (sI-A)^-1
  146. INPUTS: none (assumes INITIALIZE returned "OK")
  147. OUTPUTS: 2: a matrix polynomial list, Adj(sI-A)
  148.          1: a scalar polynomial list, det(sI-A)
  149.          [ (sI-A)^-1 = adj(sI-A)/det(sI-A) ]
  150. COMMENTS: This is just a stripped-down version of TFNCAL. Because
  151.           this was so easy to reduce (and is quite fast), I don't
  152.           care that TFNCAL would be equivalent to the following:
  153.           << ADJ SWAP C PREM B POST SWAP >>.  I just decided that
  154.           modularity can take a backseat so speed; I'm quite 
  155.           impatient during a test... 
  156.           Again, POLY does not work with polynomial lists whose
  157.           entries (coefficients) are matrices.  EPOLY does, however,
  158.           because it works for a single value.
  159. << {} { 1 } 'tmp' 0 CON 1 -> ai
  160.    << 1 ns
  161.       FOR J ai idt * 'tmp' STO+ SWAP tmp 'tmp' A STO* 0 1 ns
  162.          FOR K 'tmp' { K K } GET +
  163.          NEXT J NEG / 'ai' STO + SWAP ai +
  164.       NEXT
  165.    >>
  166. >>
  167.  
  168. OBSR--computes the observability matrix of { A,C }
  169. INPUTS: none (assumes INITIALIZE returned "OK")
  170. OUTPUTS: TRN([ C AC A^2C ... (A^(ns-1))C ])
  171. COMMENTS: not much.  Even works with multi-ouput systems...
  172. << C -> X
  173.    << X ARRY-> DROP 2 ns
  174.       START X A * DUP 'X' STO ARRY-> DROP
  175.       NEXT ns no * ns 2 ->LIST ->ARRY
  176.    >>
  177. >>
  178.  
  179. CNTR--computes the controllability matrix of { A,B }
  180. INPUTS: none (assumes INITIALIZE returned "OK")
  181. OUTPUTS: [ B AB A^2B ... (A^(ns-1))B ]] 
  182. COMMENTS: Works for multi-input systems
  183. << B { ns ni } RDM -> X
  184.    << X TRN ARRY-> DROP 2 ns
  185.       START A X * DUP 'X' STO TRN ARRY-> DROP
  186.       NEXT ns ni * ns 2 ->LIST ->ARRY TRN
  187.    >>
  188. >>
  189.  
  190. FBGN--calculates the state feedback gains required to
  191.       achieve the movement of poles which result in the
  192.       given characteristic polynomial.  In other words,
  193.       this solves for k where det(sI-A+bk)=np.
  194. INPUTS: 1: np - a polynomial list for the new
  195.                 characteristic polynomial.
  196. OUTPUTS: 1: k - the state-combination vector k.
  197. COMMENTS: Sorry, this only works for a single-input,
  198.           single-output system.  There are simply too
  199.           many degrees of freedom for me to handle in
  200.           the multi-input case.  In addition, this only
  201.           works if the system is controllable.  I don't
  202.           know what would happen if you specified a 
  203.           polynomial of degree less than the number of states!
  204. << -> np
  205.    << { 1 ns } 0 CON ns 1 PUT CNTR INV * np A EPOLY *
  206.    >>
  207. >>
  208.  
  209. SNDV--performs synthetic division on a polynomial list
  210. INPUTS: 2: the polynomial in question, L(x)
  211.         1: the root R to try to divide into L(x)
  212. OUTPUTS: 2: the divided polynomial QU(x) 
  213.          1: the remainder of the division, RM
  214. COMMENTS: you can interpret the results of the division
  215.           as L(x)=(x-R)*QU(x)+RM.  So, if level 1 contains
  216.           a zero, you've just found a root!
  217. << -> L R
  218.    << L SIZE -> N
  219.       << 0 -> K
  220.          << 1 N
  221.             FOR I 'L' I GET R K * + DUP 'K' STO
  222.             NEXT
  223.          >> -> RM
  224.          << N 1 - ->LIST RM
  225.          >>
  226.       >>
  227.    >>
  228. >>
  229.  
  230. POLY--returns a polynomal algebraic from a polynomial list
  231. INPUTS: 2: the polynomial list to convert, L(x)
  232.         1: the algebraic to substitute into the polynomial, V
  233. OUTPUTS: 1: the polynomial algebraic L(V)
  234. COMMENTS: One would most likely use this to turn a 
  235.           polynomial list into a SOLVR or PLOT-compatible
  236.           algebraic object.
  237. << -> L V
  238.    << L SIZE -> N
  239.       << 0 1 N
  240.          FOR I 'L' I GET V N I - ^ * +
  241.          NEXT
  242.       >>
  243.    >>
  244. >>
  245.  
  246. EPOLY--evaluate a polynomial list at the given value
  247. INPUTS: 2: the polynomial list in question, L(x)
  248.         1: the value to substitute in, M
  249. OUTPUTS: the calculated value, L(M)
  250. COMMENTS: the nice thing about this procedure is that
  251.           it will work not only for scalars but for
  252.           square matrices.
  253. << -> L M
  254.    << L SIZE
  255.       IF M TYPE DUP 3 == SWAP 4 == OR
  256.       THEN M IDM
  257.       ELSE 1
  258.       END -> N I
  259.       << 0 1 N
  260.          FOR J M * 'L' J GET I * +
  261.          NEXT
  262.       >>
  263.    >>
  264. >>
  265.  
  266. POLCH--get a single value from each matrix in a list
  267. INPUTS: 2: a list of vectors or matrices, L
  268.         1: the index list which results in a
  269.            valid GET command for each matrix, N
  270. OUTPUTS: { (L(1))(N) (L(2))(N) (L(3))(N) ... (L(n))(N) }
  271. COMMENTS: You may be wondering why this is useful...the
  272.           transfer function for a multi-input, multi-output
  273.           system has a transfer function MATRIX: the numerator
  274.           contains polynomials of s whose coefficients are
  275.           matrices!  Using POLCH will allow you to extract
  276.           and individual transfer function from a single input
  277.           to a single output.  For example, to find the transfer
  278.           function to output 3 from input 2, use { 3 2 } as your
  279.           parameter N.  In general, use { output# input# } as
  280.           your input.
  281. << -> L N
  282.    << L 1 L SIZE
  283.       FOR J 'L' J GET N GET J SWAP PUT
  284.       NEXT
  285.    >>
  286. >>
  287.  
  288. PREM--Pre-multiply a list of matrices by a value
  289. INPUTS: 2: The list of scalars or matrices in question, L
  290.         1: the scalar or matrix to multiply by, M
  291. OUTPUTS: { M*L(1) M*L(2) ... M*L(n) }
  292. COMMENTS: the ADJ function returns a list of matrices,
  293.           so you may wish to use this function to pre-
  294.           multiply by your row-matrices.
  295. << -> L M
  296.    << L 1 L SIZE
  297.       FOR J M 'L' J GET *
  298.          IFERR DUP SIZE
  299.          THEN
  300.          ELSE
  301.             IF DUP { 1 } == SWAP { 1 1 } == OR
  302.             THEN 1 GET
  303.             END
  304.          END J SWAP PUT
  305.       NEXT
  306.    >>
  307. >>
  308.  
  309. POSTM--post-multiply a list of scalars or matrices
  310. INPUTS: 2: the list of scalars or matrices L
  311.         1: the scalar or matrix to post-multiply, M
  312. OUTPUTS: { L(1)*M L(2)*M L(3)*M ... L(n)*M }
  313. COMMENTS: See PREM, ADJ, and TFNCAL.
  314. << -> L M
  315.    << L 1 L SIZE
  316.       FOR J 'L' J GET M *
  317.          IFERR DUP SIZE
  318.          THEN
  319.          ELSE
  320.             IF DUP { 1 } == SWAP { 1 1 } == OR
  321.             THEN 1 GET
  322.             END
  323.          END J SWAP PUT
  324.       NEXT
  325.     >>
  326. >>
  327.  
  328. These variables are updated by INITIALIZE, TFNCAL, etc. and are
  329. necessary for their operation.  While INITIALIZE will create them
  330. if necessary, it is advisable to create them yourself so that
  331. you can ORDER them to the end of your directory (out of sight).
  332. [[0]]
  333. 'tmp' STO
  334. [[1]]
  335. 'idt' STO
  336. 1
  337. 'ns' STO
  338. 1
  339. 'no' STO
  340. 1
  341. 'ni' STO
  342. ---
  343.